﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using Model;
using Common;
namespace DAL
{
    public class MenuDAL
    {
        public static int Save(MenuInfo info)
        {
            string sql = string.Format("insert into [menu]([ParentID],[Name],[Path],[Btns],[Sort],[IsShow]) values(@ParentID,@Name,@Path,@Btns,@Sort,@IsShow);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ParentID", info.ParentID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Path", info.Path),
                new SqlParameter("@Btns", info.Btns),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@IsShow", info.IsShow)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(MenuInfo info)
        {
            string sql = string.Format("update [menu] set [ParentID]=@ParentID,[Name]=@Name,[Path]=@Path,[Btns]=@Btns,[Sort]=@Sort,[IsShow]=@IsShow where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ParentID", info.ParentID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Path", info.Path),
                new SqlParameter("@Btns", info.Btns),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@IsShow", info.IsShow),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static MenuInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[menu]");
            return Simple_Data(dr);
        }
        public static MenuInfo FindOneByPath(string path)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select * from [menu] where charindex(Path,'{0}')>0",path));
            return Simple_Data(dr);
        }
        public static List<MenuInfo> FindAll()
        {

            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [menu] where IsShow=1 order by Sort asc"));
            List<BtnsInfo> btnslist = BtnsDAL.List;
            List<MenuInfo> list = new List<MenuInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row, btnslist));
            }
            return list;
        }
        public static List<MenuInfo> FindAllOfShow()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [menu] where IsShow=1 order by Sort asc"));
            List<BtnsInfo> btnslist = BtnsDAL.List;
            List<MenuInfo> list = new List<MenuInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row, btnslist));
            }
            return list;
        }
        public static List<MenuInfo> FindByParentID(int parentid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [menu] where ParentID={0} order by Sort asc",parentid));
            List<MenuInfo> list = new List<MenuInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static string Powers(string method)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            MenuInfo info = FindOneByPath(path);
            List<BtnsInfo> btnslist = BtnsDAL.List.Where(x => info.Btns.Split(',').Contains(x.ID.ToString())).ToList();
            StringBuilder build = new StringBuilder();
            AdminInfo admin = B.MyAdmin;
            string[] power = admin.Power.Split(',');
            foreach (BtnsInfo btn in btnslist)
            {
                if (method.Contains(btn.Method))
                {
                    if (power.Contains("btns-" + info.ID + "-" + btn.ID) || admin.ID == 1)
                    {
                        if (btn.Method == "Save" || btn.Method=="Export")
                        {
                            build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-small\" style=\"background-color:" + btn.Color + "\" onclick=\"" + btn.Method + "(this,0)\">" + btn.Name + "</a>&nbsp;");
                        }
                        else if (btn.Method == "Edit")
                        {
                            build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini\" style=\"background-color:" + btn.Color + "\" onclick=\"Save(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;");
                        }
                        else
                        {
                            if (info.Path.Contains("adminlist") && (btn.Method == "Power" || btn.Method=="Delete"))
                            {
                                build.Append("{{# if(d.ID!=1){ }}<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini\" style=\"background-color:" + btn.Color + "\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;{{# } }}");
                            }
                            else
                            {
                                build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini\" style=\"background-color:"+btn.Color+"\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;");
                            }
                        }
                    }
                }
            }
            return build.ToString();
        }
        public static string Power(string method)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            MenuInfo info = FindOneByPath(path);
            List<BtnsInfo> btnslist = BtnsDAL.List.Where(x => info.Btns.Split(',').Contains(x.ID.ToString())).ToList();
            StringBuilder build = new StringBuilder();
            AdminInfo admin=B.MyAdmin;
            string[] power=admin.Power.Split(',');
            if (method == "Save")
            {
                BtnsInfo btn = B.Instance<BtnsInfo>(btnslist.FirstOrDefault(x=>x.Method==method));
                if ((!string.IsNullOrEmpty(btn.Method) && power.Contains("btns-"+info.ID+"-"+btn.ID)) || admin.ID==1)
                {
                    build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-small\" onclick=\"" + btn.Method + "(this,0)\">" + btn.Name + "</a>&nbsp;");
                }
            }
            if (method == "Export")
            {
                BtnsInfo btn = B.Instance<BtnsInfo>(btnslist.FirstOrDefault(x => x.Method == method && x.Name == "导出"));
                if ((!string.IsNullOrEmpty(btn.Method) && power.Contains("btns-" + info.ID + "-" + btn.ID)) || admin.ID == 1)
                {
                    build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-small\" onclick=\"" + btn.Method + "(this,0)\">导&nbsp;出</a>&nbsp;");
                }
            }
            if(method == "")
            {
                btnslist = btnslist.Where(x => x.Name != "新增" && x.Name != "导出").ToList();
                foreach (BtnsInfo btn in btnslist)
                {
                    if (power.Contains("btns-" + info.ID + "-" + btn.ID) || admin.ID == 1)
                    {
                        if (btn.Method == "Edit")
                        {
                            build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini layui-btn-normal\" onclick=\"Save(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;");
                        }
                        else if (btn.Method == "Delete")
                        {
                            if (info.Path.Contains("adminlist"))
                            {
                                build.Append("{{# if(d.ID!=1){ }}<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini layui-btn-danger\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;{{# } }}");
                            }
                            else
                            {
                                build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini layui-btn-danger\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;");
                            }
                        }
                        else
                        {
                            if (info.Path.Contains("adminlist") && btn.Method=="Power")
                            {
                                build.Append("{{# if(d.ID!=1){ }}<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;{{# } }}");
                            }
                            else
                            {
                                build.Append("<a href=\"javascript:void(0)\" class=\"layui-btn layui-btn-mini\" onclick=\"" + btn.Method + "(this,{{d.ID}})\">" + btn.Name + "</a>&nbsp;");
                            }
                        }
                    }
                }
            }
            return build.ToString();
        }
        private static MenuInfo Simple_Data(DataRow dr)
        {
            MenuInfo info = new MenuInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Path = DataConvert.ToString(dr["Path"]);
                info.Btns = DataConvert.ToString(dr["Btns"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.IsShow = DataConvert.ToInt(dr["IsShow"]);
            }
            return info;
        }

        private static MenuInfo All_Data(DataRow dr, List<BtnsInfo> btnslist)
        {
            MenuInfo info = new MenuInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Path = DataConvert.ToString(dr["Path"]);
                info.Btns = DataConvert.ToString(dr["Btns"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.IsShow = DataConvert.ToInt(dr["IsShow"]);

                info.BtnsText = string.Join(",", btnslist.Where(x=>info.Btns.Split(',').Contains(x.ID.ToString())).Select(x=>x.Name));
            }
            return info;
        }
    }
}
